.TH std::unique_ptr::operator*,std::unique_ptr::operator-> 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::unique_ptr::operator*,std::unique_ptr::operator-> \- std::unique_ptr::operator*,std::unique_ptr::operator->

.SH Synopsis
   typename std::add_lvalue_reference<T>::type operator*() const     \fI(since C++11)\fP
       noexcept(noexcept(*std::declval<pointer>()));             \fB(1)\fP (constexpr since
                                                                     C++23)
                                                                     \fI(since C++11)\fP
   pointer operator->() const noexcept;                          \fB(2)\fP (constexpr since
                                                                     C++23)

   operator* and operator-> provide access to the object owned by *this.

   The behavior is undefined if get() == nullptr.

   These member functions are only provided for unique_ptr for the single objects i.e.
   the primary template.

.SH Parameters

   \fI(none)\fP

.SH Return value

   1) Returns the object owned by *this, equivalent to *get().
   2) Returns a pointer to the object owned by *this, i.e. get().

.SH Exceptions

   1) May throw if pointer has a throwing operator*.

.SH Notes

   The use of std::add_lvalue_reference is to make it possible to instantiate
   std::unique_ptr<void> since void& isn't allowed in C++ while
   std::add_lvalue_reference<void> produces void. See LWG673 for details.

.SH Example


// Run this code

 #include <iostream>
 #include <memory>

 struct Foo
 {
     void bar() { std::cout << "Foo::bar\\n"; }
 };

 void f(const Foo&)
 {
     std::cout << "f(const Foo&)\\n";
 }

 int main()
 {
     std::unique_ptr<Foo> ptr(new Foo);

     ptr->bar();
     f(*ptr);
 }

.SH Output:

 Foo::bar
 f(const Foo&)

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to          Behavior as published              Correct behavior
                       operator* might be potentially-throwing  a conditional exception
   LWG 2762 C++11      even if                                  specification added
                       *get() was noexcept

.SH See also

   get returns a pointer to the managed object
       \fI(public member function)\fP
